package org.dromara.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.api.model.LoginUser;
import org.springframework.stereotype.Service;
import org.dromara.customer.domain.bo.TaAddressBo;
import org.dromara.customer.domain.vo.TaAddressVo;
import org.dromara.customer.domain.TaAddress;
import org.dromara.customer.mapper.TaAddressMapper;
import org.dromara.customer.service.ITaAddressService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 地址Service业务层处理
 *
 * @author xiyin321
 * @date 2024-07-07
 */
@RequiredArgsConstructor
@Service
public class TaAddressServiceImpl implements ITaAddressService {

    private final TaAddressMapper baseMapper;

    /**
     * 查询地址
     */
    @Override
    public TaAddressVo queryById(Long addressId) {
        return baseMapper.selectVoById(addressId);
    }

    /**
     * 查询地址列表
     */
    @Override
    public TableDataInfo<TaAddressVo> queryPageList(TaAddressBo bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setUserId(loginUser.getUserId());
        Page<TaAddressVo> result = baseMapper.selectAddressPage(pageQuery.build(), this.buildQueryWrapper(bo));
        return TableDataInfo.build(result);
    }

    /**
     * 查询地址列表
     */
    @Override
    public List<TaAddressVo> queryList(TaAddressBo bo) {
        return baseMapper.selectVoList(this.buildQueryWrapper(bo));
    }

    /*构建查询条件*/
    private Wrapper<TaAddress> buildQueryWrapper(TaAddressBo bo) {
        QueryWrapper<TaAddress> wrapper = Wrappers.query();
        wrapper.eq("del_flag", "0")
            .eq("user_id", bo.getUserId());
        return wrapper;
    }

    /**
     * 新增地址
     */
    @Override
    public Boolean insertByBo(TaAddressBo bo) {
        TaAddress add = MapstructUtils.convert(bo, TaAddress.class);
        validEntityBeforeSave(add);
        /*获取当前登入用户ID*/
        LoginUser loginUser = LoginHelper.getLoginUser();
        add.setUserId(loginUser.getUserId());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAddressId(add.getAddressId());
        }
        return flag;
    }

    /**
     * 修改地址
     */
    @Override
    public Boolean updateByBo(TaAddressBo bo) {
        TaAddress update = MapstructUtils.convert(bo, TaAddress.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TaAddress entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除地址
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
